package com.atheima.reggie.controller;

import com.atheima.reggie.common.BaseContext;
import com.atheima.reggie.common.R;
import com.atheima.reggie.common.ValidateUtil;
import com.atheima.reggie.entity.AddressBook;
import com.atheima.reggie.entity.Orders;
import com.atheima.reggie.entity.ShoppingCart;
import com.atheima.reggie.service.AddressBookService;
import com.atheima.reggie.service.OrderService;
import com.atheima.reggie.service.ShoppingCartService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private AddressBookService addressBookService;

    @PostMapping("/submit")
    public R submit(@RequestBody Orders orders) {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);

        if(ValidateUtil.isEmpty(list)) {
            return R.error("购物车不能为空");
        }
        Long addressBookId = orders.getAddressBookId();
        AddressBook addressBook = addressBookService.getById(addressBookId);
        if(addressBook == null) {
            return R.error("地址不对");
        }
        return R.ok();
    }
}
